package com.example.droolstest.service.impl;

import com.example.droolstest.entity.RuleVersion;
import com.example.droolstest.entity.req.ExecuteRuleReq;
import com.example.droolstest.exception.ServiceException;
import com.example.droolstest.service.DroolsService;
import com.example.droolstest.service.RuleVersionService;
import com.example.droolstest.util.DroolsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class DroolsServiceImpl implements DroolsService {
    @Autowired
    private RuleVersionService ruleVersionService;
    @Override
    public Object execute(ExecuteRuleReq executeRule) {
        RuleVersion ruleVersion = ruleVersionService.getById(executeRule.getRuleVersionId());
        if(ruleVersion == null){
            throw new ServiceException("不存在的规则版本.");
        }
        DroolsUtil.execute(executeRule.getParams(), ruleVersion.getInfo());
        return executeRule.getParams().get("result");
    }
}
